/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.notaria.guerrasalas.bc;

import com.notaria.guerrasalas.be.EstadoEntidad;
import com.notaria.guerrasalas.be.Persona;
import gob.satt.comun.jpa.BadUpdateException;
import gob.satt.comun.jpa.NotFoundException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrador
 */
public class PersonaDelegate extends AbstractDelegate{

    private GuerraSalasServiceLocal local;

    public PersonaDelegate() {
        local = lookupGuerraSalasServiceBean();
    }

    public void crear(Persona persona) throws BadUpdateException {
        try {
            local.create(persona);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Crear Persona: " + getMensajeError(e), e);
        }
    }

    public void editar(Persona persona) throws BadUpdateException {
        try {
            local.edit(persona);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Editar Persona: " + getMensajeError(e), e);
        }
    }

    public void eliminar(Persona persona) throws BadUpdateException {
        try {
            persona.setEstado(EstadoEntidad.Inactivo);
            persona.setFechaActualizacion(new Date());
            persona.setFechaEliminacion(new Date());
            local.remove(persona);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Eliminar Persona: " + getMensajeError(e), e);
        }
    }

    public Persona buscar(Long codigo) throws NotFoundException {
        try {
            return (Persona) local.find(codigo, Persona.class);
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Buscar Persona: " + getMensajeError(e), e);
        }
    }

    public List<Persona> listar(String nombresApellidos) throws NotFoundException {
        try {
            return (List<Persona>) local.execute(new ListarPersonasCommand(nombresApellidos));
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Listar Personas: " + getMensajeError(e), e);
        }
    }

    public Persona buscarPersonaYaRegistrada(String nombres,String apellidoPaterno,String apellidoMaterno,String tipo,boolean isEmpleado) throws NotFoundException {
        try {
            return (Persona) local.execute(new BuscarPersonaYaRegistradaCommand(nombres, apellidoPaterno, apellidoMaterno, tipo,isEmpleado));
        } catch (Exception e) {
            local = lookupGuerraSalasServiceBean();
            throw new NotFoundException("Buscar persona ya registrada: " + getMensajeError(e), e);
        }
    }

    private GuerraSalasServiceLocal lookupGuerraSalasServiceBean() {
        try {
            Context c = new InitialContext();
            return (GuerraSalasServiceLocal) c.lookup("java:comp/env/GuerraSalasServiceBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
